import 'dart:typed_data';

import 'package:flutter_blue_plus/flutter_blue_plus.dart';

import 'base_service.dart';

class BatteryService extends BaseService {
  static const serviceGuid = ("0000180f-0000-1000-8000-00805f9b34fb");
  static const guidCharacteristicWrite = ("00002a19-0000-1000-8000-00805f9b34fb");

  BatteryService()
      : super(
      serviceGuid: serviceGuid,
      characteristicNotifyGuid: null,
      characteristicReadGuid: null,
      characteristicWriteGuid: guidCharacteristicWrite);

  @override
  Future onCleanup() async{

  }

  @override
  Future onInitialized(List<BluetoothService> services) async{

  }

  Future<num?> readBattery() async {
    try {
      final data = await writeCharacteristic?.read();
      final uint8List = Uint8List.fromList(data ?? []);
      return uint8List.buffer.asByteData().getUint8(0);
    } catch (e) {
      return null;
    }
  }
}